<?php

namespace app\home\controller;

use think\Controller;
use think\Request;
use app\admin\model\Goods;
use app\home\model\GoodsSkuAttr;
use app\home\model\GoodsSku;
use app\home\model\Cart;
use think\db\Where;
use Db;


class Search extends Controller
{   
    protected $middleware = [
        'Check'=> ['except' 	=> ['index','read'] ],
    ];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $req)
    {   
        $list = Goods::where('type3_id',$req->type_id)->paginate(10);

        return view('search',['list'=>$list]);
    }

    //ajax 根据实时传递过来的skuid 查询商品sku
    public function ajax_sku(Request $req)
    {
        // var_dump($req->attr);
        $pro =  implode(";",$req->attr);

        $data = GoodsSku::where('sku_attr',$pro)->select();

        if($data){
            return json($data);
        }else{
            return '请输入完整规格';
        }
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $req,$spu_id)
    {   
        // var_dump($spu_id);die;
        //获取商品的sku属性名
        // select b.sku_key,a.sku_key_id from goods_sku_attr a  LEFT JOIN sku_key b on a.sku_key_id = b.id GROUP BY a.sku_key_id
        $data['keyid'] = GoodsSkuAttr::where(1)->alias('a')->field('b.sku_key,a.sku_key_id')->join('sku_key b','a.sku_key_id = b.id')->group('a.sku_key_id')->select();
        // echo '<pre>';
        // var_dump($data['keyid'] );
        // select b.sku_value_id,c.* from goods_sku a LEFT JOIN goods_sku_attr b on a.id = b.sku_id LEFT JOIN sku_value c on b.sku_value_id=c.id GROUP BY b.sku_value_id
        //获取商品的sku属性值
        $data['value'] = GoodsSku::where('goods_id',$spu_id)->alias('a')->field('b.sku_value_id,c.*')->join('goods_sku_attr b','a.id = b.sku_id')->join('sku_value c',' b.sku_value_id=c.id')->group('b.sku_value_id')->select();
        // var_dump($data['value']);
        //获取商品的相册及spu数据
        $data['image'] = Goods::where('spu_id',$spu_id)->alias('a')->field('a.*,b.*')->join('goods_image b','a.spu_id = b.goods_id')->select();
        //获取所有的库存
        $data['num'] = GoodsSku::where('goods_id',$spu_id)->field('sum(sku_num) num')->select();
        // echo '<pre>';
        // var_dump($data['num']);die;

        return view('read',$data);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
       
    }

    //订单结算页面
    public function getOrderInfo(Request $req)
    {   
       //获取购物车中的商品
        //Cart::select();
       $parm = $req->id;
       $name = '';
    //var_dump($arr,$parm);die;
    for($i=0;$i<count($parm);$i++){

        $name = $name."'".$parm[$i]."',";    
    }
        
       $the_uname ="id in(".$name."'')"; //减去字符串的最后一个豆号

    //var_dump($the_uname);die;
       $total_price = Db::query('select sum(cart_goods_num*cart_goods_price) total_price,sum(cart_goods_num) as total_num  from cart where '. $the_uname);
       $data['total'] = $total_price[0]['total_price'];
       $data['num'] = $total_price[0]['total_num'];
       $data['list'] = Db::query('select * from cart where '. $the_uname);
       
    // var_dump($data);die;
   
       return view('getOrderInfo',$data);
    }

}
